import { GetCol, GetNoCol, GetNameCol } from "@/api/college";
import { GetMaj, GetNoMaj, GetNameMaj } from "@/api/major";
import { GetCla, GetNoCla, GetNameCla } from "@/api/class";
import { GetStu, GetNoStu, GetNameStu } from "@/api/student";
import { GetPer, GetNoPer, GetNamePer } from "@/api/personality";
import { GetQue, GetNoQue } from "@/api/question";
import { GetRes, GetCnoRes, GetPnoRes, GetSnoRes } from "@/api/result";

const getDefaultState = () => {
    return {
        Content: [],
        Owner: ''
    }
}

const state = getDefaultState()

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    },
    SET_CONTENT: (state, content) => {
        state.Content = content
    },
    SET_OWNER: (state, source) => {
        state.Owner = source
    }
}

const actions = {
    // get all info of college
    getColAll ({ commit }) {
        return new Promise((resolve, reject) => {
            GetCol().then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get part info of college by no
    getColNo ({ commit }, info) {
        const { No } = info
        return new Promise((resolve, reject) => {
            GetNoCol({ No: No }).then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get part info of college by name
    getColName ({ commit }, info) {
        const { Name } = info
        return new Promise((resolve, reject) => {
            GetNameCol({ Name: Name }).then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get all info of major
    getMajAll ({ commit }) {
        return new Promise((resolve, reject) => {
            GetMaj().then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get part info of major by no
    getMajNo ({ commit }, info) {
        const { No } = info
        return new Promise((resolve, reject) => {
            GetNoMaj({ No: No }).then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get part info of major by name
    getMajName ({ commit }, info) {
        const { Name } = info
        return new Promise((resolve, reject) => {
            GetNameMaj({ Name: Name }).then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get all info of class
    getClaAll ({ commit }) {
        return new Promise((resolve, reject) => {
            GetCla().then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get part info of class by no
    getClaNo ({ commit }, info) {
        const { No } = info
        return new Promise((resolve, reject) => {
            GetNoCla({ No: No }).then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get part info of class by name
    getClaName ({ commit }, info) {
        const { Name } = info
        return new Promise((resolve, reject) => {
            GetNameCla({ Name: Name }).then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get all info of student
    getStuAll ({ commit }) {
        return new Promise((resolve, reject) => {
            GetStu().then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get part info of student by no
    getStuNo ({ commit }, info) {
        const { No } = info
        return new Promise((resolve, reject) => {
            GetNoStu({ No: No }).then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get part info of student by name
    getStuName ({ commit }, info) {
        const { Name } = info
        return new Promise((resolve, reject) => {
            GetNameStu({ Name: Name }).then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get all info of personality
    getPerAll ({ commit }) {
        return new Promise((resolve, reject) => {
            GetPer().then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get part info of personality by name
    getPerNo ({ commit }, info) {
        const { No } = info
        return new Promise((resolve, reject) => {
            GetNoPer({ No: No }).then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get part info of personality by name
    getPerName ({ commit }, info) {
        const { Name } = info
        return new Promise((resolve, reject) => {
            GetNamePer({ Name: Name }).then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get all info of question
    getQueAll ({ commit }) {
        return new Promise((resolve, reject) => {
            GetQue().then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get part info of question by no
    getQueNo ({ commit }, info) {
        const { No } = info
        return new Promise((resolve, reject) => {
            GetNoQue({ No: No }).then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get all info of result
    getResAll ({ commit }, info) {
        const { Sno, Pno } = info
        return new Promise((resolve, reject) => {
            GetRes({ Sno: Sno, Pno: Pno }).then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get part info of result by personality no
    getResPno ({ commit }, info) {
        const { Sno, Pno } = info
        return new Promise((resolve, reject) => {
            GetPnoRes({ Sno: Sno, Pno: Pno }).then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get part info of result by student no
    getResSno ({ commit }, info) {
        const { Sno, Pno } = info
        return new Promise((resolve, reject) => {
            GetSnoRes({ Sno: Sno, Pno: Pno }).then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get part info of result by class no
    getResCno ({ commit }, info) {
        const { No } = info
        return new Promise((resolve, reject) => {
            GetCnoRes({ No: No }).then(response => {
                const { Data } = response
                if (!Data) {
                    return reject(response.Code)
                }
                commit('SET_CONTENT', Data.res)
                commit('SET_OWNER', Data.source)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
